﻿--[[
	VERSION 1.5.1 by Lyn
	CREDITS: evl and his awesome evl_clock to get lynstats "ace-less" 
--]]
local addon = CreateFrame("Button", LynStats, UIParent)

--
-- configuration
-- ----------------------------------------------------------------------
-- true/false options are always true = YES, false = NO
--
-- other options for frame_anchor and text_anchor are:
-- LEFT, TOPLEFT, TOP, TOPRIGHT, RIGHT, CENTER, BOTTOMRIGHT, BOTTOM, BOTTOMLEFT
--

-- frame
local frame_anchor = "BOTTOMRIGHT"
local pos_x = -10
local pos_y = 10
-- text
local text_anchor = "BOTTOMRIGHT"
local font = "Fonts\\ARIALN.TTF"
local size = 12
local classcolors = true
local customcolors = { r=0, g=0.8, b=1 } -- only if classcolors is false
local shadow = true
local outline = false
local time24 = true	-- true: 24h / false: 12h
-- display
local addonlist = 50	-- how much addons should be shown?
local show_fps = true	-- frames per second
local show_lag = true	-- latency
local show_mem = false	-- old memory display
local show_xp = true	-- experience
local show_mail = true	-- mail indicator?
local show_time = true	-- clock
local show_tip = true	-- if false it doesn't show the addon tooltip
if show_tip == true then	-- ignore this line plx plx! :D
	-- tooltip options
	local tip_anchor = "BOTTOMRIGHT"
	local tip_x = -90
	local tip_y = 35
end	-- ... and this line x)


--
--	the magic begins here!
--

local color, mail, hasmail, ticktack, lag, fps, ep, xp_cur, xp_max, text, blizz, memory, entry, i, nr, xp_rest, ep, mem

if classcolors == true then
	color = RAID_CLASS_COLORS[select(2, UnitClass("player"))]
else
	color = customcolors
end

if show_tip == true then
	local memformat = function(number)
		if number > 1000 then
			return string.format("%.2f mb", (number / 1000))
		else
			return string.format("%.1f kb", floor(number))
		end
	end

	local addoncompare = function(a, b)
		return a.memory > b.memory
	end
end

function addon:new()
	text = self:CreateFontString(nil, "OVERLAY")
	if outline == true then
		text:SetFont(font, size, "OUTLINE")
	else
		text:SetFont(font, size, nil)
	end
	if shadow == true then
		text:SetShadowOffset(1,-1)
	end
	text:SetPoint(text_anchor, self)
	text:SetTextColor(color.r, color.g, color.b)

	self:SetPoint(frame_anchor, UIParent, frame_anchor, pos_x, pos_y)
	self:SetWidth(50)
	self:SetHeight(13)
	
	self:SetScript("OnUpdate", self.update)
	if show_tip == true then
		self:SetScript("OnEnter", self.enter)
		self:SetScript("OnLeave", function() GameTooltip:Hide() end)
	end
end

local last = 0
function addon:update(elapsed)
	last = last + elapsed

	if last > 1 then
		
		if show_mail == true then
			hasmail = (HasNewMail() or 0);
			if hasmail > 0 then
				mail = "|c00FA58F4new!|r "
			else
				mail = ""
			end
		else
			mail = ""
		end

		if show_time == true then
			if time24 == true then
				ticktack = date("%H.%M")
			else
				ticktack = date("%I.%M")
			end
			ticktack = "|c00ffffff"..ticktack.."|r"
		else
			ticktack = ""
		end
		
		if show_mem == true then
			mem = collectgarbage("count")
			mem = "|c00ffffff"..floor(mem / 1024).."|rmb  "
		else
			mem = ""
		end

		if show_fps == true then
			fps = GetFramerate()
			fps = "|c00ffffff"..floor(fps).."|rfps  "
		else
			fps = ""
		end
		
		if show_lag == true then
			lag = select(3, GetNetStats())
			lag = "|c00ffffff"..lag.."|rms  "
		else
			lag = ""
		end
		
		if show_xp == true then
			xp_cur = UnitXP("player")
			xp_max = UnitXPMax("player")
			xp_rest = GetXPExhaustion("player") or nil
			if UnitLevel("player") < MAX_PLAYER_LEVEL then
				ep = "|c00ffffff"..floor(xp_max - xp_cur).."|r"
				if xp_rest ~= nil then	
					ep = ep.."|c00ffffff(|r|c0000ccffR|r|c00ffffff)|rxp"
				else
					ep = ep.."xp"
				end
			else
				ep = ""
			end
		else
			ep = ""
		end
		
		last = 0
		
		text:SetText(fps..lag..mem..ep..mail..ticktack)
		self:SetWidth(text:GetStringWidth())
	end
end

if show_tip == true then
	function addon:enter()
		GameTooltip:SetOwner(self, "ANCHOR_NONE")
		GameTooltip:SetPoint(tip_anchor, UIParent, tip_anchor, tip_x, tip_y)
		blizz = collectgarbage("count")
		addons = {}
		total = 0
		nr = 0
		UpdateAddOnMemoryUsage()
		for i=1, GetNumAddOns(), 1 do
			if (GetAddOnMemoryUsage(i) > 0 ) then
				memory = GetAddOnMemoryUsage(i)
				entry = {name = GetAddOnInfo(i), memory = memory}
				table.insert(addons, entry)
				total = total + memory
			end
		end
		table.sort(addons, addoncompare)
		for _, entry in pairs(addons) do
			if nr < addonlist then
				GameTooltip:AddDoubleLine(entry.name, memformat(entry.memory), 1, 1, 1, 1, 1, 1)
				nr = nr+1
			end
		end
		GameTooltip:AddLine(" ")
		GameTooltip:AddDoubleLine("Total", memformat(total), color.r, color.g, color.b, color.r, color.g, color.b)
		GameTooltip:AddDoubleLine("Total incl. Blizzard", memformat(blizz), color.r, color.g, color.b, color.r, color.g, color.b)
		GameTooltip:Show()
	end
end

addon:new()